package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.atguigu.tingshu.album.config.MinioConstantProperties;
import com.atguigu.tingshu.album.service.AuditService;
import com.atguigu.tingshu.album.service.FileUploadService;
import com.atguigu.tingshu.common.execption.GuiguException;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.UUID;

@Service
@Slf4j
public class FileUploadServiceImpl implements FileUploadService {

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioConstantProperties minioConstantProperties;

    @Autowired
    private AuditService auditService;
    @Override
    public String fileUpload(MultipartFile file) {
        String fileName = null;
        try {
            //1.先校验文件是否为图片
            BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
            //2.限制文件大小  要求：900*900 像素，大小不超过2M
            if (bufferedImage == null) {
                throw new GuiguException(500, "秃瓢要求不符合");
            }
            int height = bufferedImage.getHeight();
            int width = bufferedImage.getWidth();
            if (height > 900 || width > 900){
                throw new GuiguException(500,"图片过大，图片限制900*900");
            }
            //对图片内容进行校验是否合法
            String suggestion = auditService.auditImage(file);
            if ("review".equals(suggestion) || "block".equals(suggestion)) {
                throw new GuiguException(500, "图片内容不合法");
            }
            //3.调用MinioCLient对象上传文件
            //3.1生成唯一标识
            String folderName = "/" + DateUtil.today();

            String extedName = FileUtil.extName(file.getOriginalFilename());
            //3.2生成文件名
            fileName = folderName + "/" + UUID.randomUUID().toString() + extedName;
            minioClient.putObject(
                    PutObjectArgs.builder().bucket(minioConstantProperties.getBucketName()).object(fileName).stream(
                                    file.getInputStream(), file.getSize(), -1)
                            .contentType("video/mp4")
                            .build());
        }  catch (Exception e) {
            log.error("文件上传失败",e);
            throw new GuiguException(500,e.getMessage());
        }
        //最后生成的文件访问路径为http://192.168.200.6/tingshu/20250528/fdhsajkhfjkdshakl
        //4.返回文件路径
        return minioConstantProperties.getEndpointUrl() +"/" +  minioConstantProperties.getBucketName() + fileName;
    }
    //解除上传文件大小限制，在nacos中添加配置信息
    //    spring:
    //    servlet:
    //    multipart:
    //    max-file-size: 10MB     #单个文件最大限制
    //    max-request-size: 20MB  #多个文件最大限制
}